<template>
	<view class="userLogin">
		<image class="userPhoto" src="/static/user/default.jpg"></image>
		<button type="primary" class="login" open-type="getUserInfo" @getuserinfo="getUserInfo">一键登录</button>
		<text class="describe">登录后尽享更多权限</text>
	</view>
</template>

<script>
	import { mapState,mapMutations } from 'vuex'
	export default {
		name:"my-login",
		data() {
			return {
				
			};
		},
		computed: {
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo','updateToken','jumpTo']),
			getUserInfo(e){
				const { detail } = e
				if(detail.errMsg !== "getUserInfo:ok") return uni.$showMsg('登录授权失败')
				this.updateUserInfo(detail.userInfo)
				this.getToken(detail)
			},
			async getToken(info){
				const res = await uni.login().catch(err => err)
				if(res.errMsg !== "login:ok") return uni.$showMsg('登录失败')
				const query = {
					encryptedData: info.encryptedData,
					rawData: info.rawData,
					iv: info.iv,
					signature: info.signature,
					code: res.code
				}
				const {data: loginResult } = await uni.$http.post('/users/wxlogin', query)
				// if(loginResult.meta.status !== 200) return uni.$showMsg('登录失败')
				this.updateToken('Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo')
				uni.$showMsg('登录成功')
				this.jumpTo(-1)
			}
		}
	}
</script>

<style lang="scss">
.userLogin{
	height: 750rpx;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
	position: relative;
	overflow: hidden;
	background-color: #f8f8f8;
	&::after{
		content: '';
		width: 100%;
		height: 40px;
		border-radius: 100%;
		background-color: #767676;
		position: absolute;
		bottom: 0;
		transform: translateY(50%);
		background-color: white;
	}
	.userPhoto{
		width: 100px;
		height: 100px;
		display: block;
	}
	.login{
		width: 80%;
		background-color: #C00000;
		border-radius: 25px;
		color: white;
		margin: 25px 0 15px;
	}
	.describe{
		color: #767676;
		font-size: 12px;
	}
}
</style>